package web.admin.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.Role;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.CriteriaRole;
import services.RoleCRUDService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 24/08/12
 * Time: 15:21
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/Admin/Role")
public class RoleController extends ActionSupport implements ModelDriven {

    private RoleCRUDService roleCRUDService;
    private Role role = new Role();
    private List<Role> list;
    private Integer roleId;
    private List<String> opciones;
    private String check;

    private static final String SITE = "Site Staff";
    private static final String BMS = "BMS Staff";

    @Autowired
    public RoleController(RoleCRUDService roleCRUDService){
        this.roleCRUDService = roleCRUDService;
        opciones = new ArrayList<String>();
        opciones.add(SITE);
        opciones.add(BMS);
    }

    @Override
    public Object getModel() {
        return role;
    }

    @Action(value="main", results={
            @Result(name="success", location="/directories.role.tiles", type="tiles")
    })
    public String execute() {
        list = new ArrayList<Role>(0);
        return SUCCESS;
    }

    @Action(value="list", results={
            @Result(name="success", location="/directories.role.tiles", type="tiles")
    })
    public String list() {

        CriteriaRole crS = new CriteriaRole(role);
        list = roleCRUDService.list(crS);
        return SUCCESS;
    }

    @Action(value="create", results={
            @Result(name="success",location="/directories.role.create.tiles", type="tiles")
    })
    public String createRole() {

        if(roleId != null){
            role = roleCRUDService.getRoleById(roleId);
               if(role.getBmsstaff())
                   check = BMS;
                else
                   check = SITE;
        }
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String saveRole() {

       if( check.equals(SITE))   {
           role.setBmsstaff(false);
           role.setSiteStaff(true);
       }else{
           role.setSiteStaff(false);
           role.setBmsstaff(true);
       }

        roleCRUDService.save(role);


        return SUCCESS;
    }

    @Action(value="delete", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String deleteRole() {

        roleCRUDService.delete(role);

        return SUCCESS;
    }


    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public List<Role> getList() {
        return list;
    }

    public void setList(List<Role> list) {
        this.list = list;
    }
    public List<String> getOpciones() {
        return opciones;
    }

    public void setOpciones(List<String> opciones) {
        this.opciones = opciones;
    }

    public String getCheck() {
        return check;
    }

    public void setCheck(String check) {
        this.check = check;
    }
}
